var rootConfig = {
	moduleDir : 'modules',
	baseUrl : 'http://localhost/cafe-control-new/api/index.php'
}

var rootApp = angular.module('rootApp',['ui.router','ui.bootstrap','auth','home','control']);
rootApp.config(function ($stateProvider, $urlRouterProvider,$provide) {
	$urlRouterProvider.otherwise("/home");
	$stateProvider
	    .state(rootApp.name, {
	      url: '/error-404.html',
	      templateUrl: ['error-404.html'].join('/'),
	    });
    $provide.decorator('$state', function($delegate, $stateParams) {
        $delegate.reload = function() {
            return $delegate.go($delegate.current, $stateParams, {
                reload: true,
                inherit: false,
                notify: true
            });
        };
        return $delegate;
    });
});
rootApp.run(function($rootScope,$location,$http,$state,$stateParams){
	/* set public $state*/
	$rootScope.$state = $state;
	$rootScope.$stateParams = $stateParams;
	$rootScope.menu = [];
	/* check auth on app run or router change */
	var url = [rootConfig.baseUrl,'users/users/check_auth'].join('/');
	$rootScope.$on('$stateChangeStart',function(){
		$http.get(url).success(function(data){
			if(data.status==false){
				$location.path('/login');
			}else{
				$rootScope.user = data.data;
			}
		});
	});
	$rootScope.$on('$stateNotFound',function(){
		$location.path('/error-404.html');
	});
	$rootScope.$on('Menu',function(event,data){
		$rootScope.menu.push(data);
	}); 
});
rootApp.service('rootService', ['$http',function($http){
	return {
		validateForm : function(formData){
			var flag = true;
			if(angular.isObject(formData) && flag){
				angular.forEach(formData, function(value,key){
					if(angular.isUndefined(value) || value==''){
						flag =  false;
					}
				});
			}else flag = false;
			return flag;
		},
	}
}]);
rootConfig.loading = function (flag, parent)
{
    if (flag)
    {
        $(parent).append('<div class="message-loading-overlay"><i class="fa-spin ace-icon fa fa-spinner blue bigger-160"></i></div>');
    }
    else
    {
        $('.message-loading-overlay').remove();
    }
}

/* Set public box modal */
var box = {
	alert: function(contents,_class,title){
		var opt = {};
		if(title) opt.title = title;
		if(contents) opt.text = contents;
		if(_class) opt.class_name = _class;
		else opt.class_name = 'gritter-info';
		$.gritter.add(opt);
	},
    confirm : function(contents,title,callback){
        $('body').append('<div class="message-loading-overlay"></div>');
        var opt = {};
        if(title) opt.title = title;
        opt.class_name = 'gritter-info gritter-center';
        var html = [
            contents,
            '<hr/>',
            '<div class="col-md-4 pull-right"><button id="ok" class="btn btn-primary btn-block">OK</button></div>',
            '<div class="col-md-4 pull-right"><button id="cancel" class="btn btn-default btn-block">Hủy</button></div>',
        ].join('');
        opt.text = html;
        opt.sticky = true;
        opt.after_close = function(){
            $('.message-loading-overlay').remove();
        }
        var $this = $.gritter.add(opt);
        $('button#ok').on('click',function(){
            $.gritter.remove($this);
            callback(true);
        });
        $('button#cancel').on('click',function(){
            $.gritter.remove($this);
            callback(false);
        });
    }
}
var myModal = function(){
	if($('#my-modal').length<=0){
		var html = [
			'<div id="my-modal" class="my-modal">',
				'<div class="footer">',
					'<a>Đóng</a>',
				'</div>',
			'</div>'
		].join('');
		$('body').append(html);
		function close(flag){
			if(flag){
				$('#my-modal').remove();
			}
		}
	}
}